# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug-git")
pkgver=r9618.89481ab
pkgrel=1
arch=('any')
license=('LGPL')
url="http://osgearth.org"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "git")
options=('staticlibs' 'strip')
source=("${_realname}"::"git+https://github.com/gwaldron/osgearth.git")
md5sums=('SKIP')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}
}

build() {
  _pkgver=${_realname}-${pkgver} # How it's tagged in git

  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir}
    pushd ${builddir}
      [[ "${builddir%-${MINGW_CHOST}}" == "Release" ]] && {
        local _opts=""
      } || {
        local _opts=""
      }

      msg "Building ${builddir%-${MINGW_CHOST}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"MSYS Makefiles" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MINGW_CHOST}} \
        -DOSG_DIR=${MINGW_PREFIX} \
        -DOSGEARTH_QT_BUILD=ON \
        ${_opts} \
        ../${_realname}

      make
    popd
  done
}

package_release() {
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
           "${MINGW_PACKAGE_PREFIX}-gdal")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd Release-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install
}

package_debug() {
  pkgdesc="A terrain rendering toolkit for OpenSceneGraph (debug) (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}-git=$pkgver"
           "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph-debug")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
  options=('staticlibs' '!strip')

  cd Debug-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share
}

package_mingw-w64-i686-osgearth-git() {
  package_release
}

package_mingw-w64-i686-osgearth-debug-git() {
  package_debug
}

package_mingw-w64-x86_64-osgearth-git() {
  package_release
}

package_mingw-w64-x86_64-osgearth-debug-git() {
  package_debug
}
